package com.minhua.jijiehao2.mainframe.circle.productIdentifyActivity.impl

import android.app.Activity
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.text.Editable
import android.view.View
import android.widget.TextView
import com.google.gson.Gson
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseActivity
import com.minhua.jijiehao2.base.adapter.ViewHolder
import com.minhua.jijiehao2.base.adapter.recyclerview.BaseCommonAdapter
import com.minhua.jijiehao2.bean.ContactsBean
import com.minhua.jijiehao2.mainframe.circle.productIdentifyActivity.imp.IProductIdentifyView
import com.minhua.jijiehao2.mainframe.circle.productServiceActivity.impl.ProductServiceActivity
import com.minhua.jijiehao2.mainframe.circle.productTypeActivity.impl.ProductTypeActivity
import com.minhua.jijiehao2.utils.AfterTextWatcher
import kotlinx.android.synthetic.main.activity_product_identify.*

class ProductIdentifyActivity : BaseActivity<IProductIdentifyView, ProductIdentifyPresenter>(), IProductIdentifyView {

    private val resultProductService = 1
    private val resultProductType = 2

    override fun getLayoutId(): Int = R.layout.activity_product_identify

    override fun getPresenter(): ProductIdentifyPresenter = ProductIdentifyPresenter()

    private val mOnClickListener = View.OnClickListener { v ->
        when (v.id) {
            R.id.tvProductService -> ProductServiceActivity.intentStart(this, "产品服务介绍", introduce, resultProductService)
            R.id.tvType -> startActivityForResult(Intent(this, ProductTypeActivity::class.java), resultProductType)
            R.id.tvSubmit -> {
//                T.showLong(listContacts.toString())
//                startActivity(Intent(this, ProductResultActivity::class.java))


            }
        }
    }

    private val listContacts = arrayListOf<ContactsBean>()
    private var adapter: BaseCommonAdapter<ContactsBean>? = null


    private var code = ""
    private var introduce = ""
    private var logoPath = ""

    override fun initView() {
        tvProductService.setOnClickListener(mOnClickListener)
        tvType.setOnClickListener(mOnClickListener)
        tvSubmit.setOnClickListener(mOnClickListener)
        listContacts.add(ContactsBean())
        adapter = object : BaseCommonAdapter<ContactsBean>(this, R.layout.item_add_contacts, listContacts) {
            override fun convert(holder: ViewHolder?, t: ContactsBean?) {
                val mPosition = holder?.getmPosition() ?: 0
                val tvAddOrDelete = holder?.getView<TextView>(R.id.tvAddOrDelete)
                if (mPosition == 0) {
                    tvAddOrDelete?.text = "添加运营信息"
                    tvAddOrDelete?.setOnClickListener {
                        listContacts.add(ContactsBean())
                        adapter?.notifyDataSetChanged()
                    }
                } else {
                    tvAddOrDelete?.text = "删除"
                    tvAddOrDelete?.setOnClickListener {
                        listContacts.removeAt(mPosition)
                        adapter?.notifyDataSetChanged()
                    }
                }

                holder?.setEditTextWatcher(R.id.tvName, object : AfterTextWatcher() {
                    override fun afterTextChanged(s: Editable?) {
                        t?.name = s.toString().trim()
                    }
                })
                holder?.setEditTextWatcher(R.id.tvPhone, object : AfterTextWatcher() {
                    override fun afterTextChanged(s: Editable?) {
                        t?.tel = s.toString().trim()
                    }
                })
                holder?.setEditTextWatcher(R.id.tvWeChat, object : AfterTextWatcher() {
                    override fun afterTextChanged(s: Editable?) {
                        t?.wx = s.toString().trim()
                    }
                })
                holder?.setEditTextWatcher(R.id.tvQQ, object : AfterTextWatcher() {
                    override fun afterTextChanged(s: Editable?) {
                        t?.qq = s.toString().trim()
                    }
                })
                holder?.setEditTextWatcher(R.id.tvJob, object : AfterTextWatcher() {
                    override fun afterTextChanged(s: Editable?) {
                        t?.job = s.toString().trim()
                    }
                })
                holder?.setEditTextWatcher(R.id.tvMajor, object : AfterTextWatcher() {
                    override fun afterTextChanged(s: Editable?) {
                        t?.module = s.toString().trim()
                    }
                })
            }
        }

        rvContacts.isNestedScrollingEnabled = false
        rvContacts.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        rvContacts.adapter = adapter

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == resultProductService) {
                introduce = data?.getStringExtra("text") ?: ""
            } else if (requestCode == resultProductType) {
                tvType.centerText = data?.getStringExtra("name") ?: ""
                code = data?.getStringExtra("code") ?: ""
            }
        }
    }


    override fun type(): String = code

    override fun org_id(): String = ""

    override fun proc_name(): String = tvProductName.text

    override fun logo_path(): String = logoPath

    override fun official_url(): String = tvWebPage.text

    override fun proc_info(): String = introduce

    override fun operativeInfos(): String = Gson().toJson(listContacts)
}
